﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp7
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            var SelectString = "select * from Articles";
            var st = DbHelper.GetDataTable(SelectString);
            dataGridView1.DataSource = st;
            dataGridView1.ReadOnly = true;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AllowUserToAddRows = false;
        }
        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            var titleName = textBox1.Text;
            var findString = string.Format("select * from Articles where Title like '%{0}%'", titleName);
            var dt = DbHelper.GetDataTable(findString);
            dataGridView1.DataSource = dt;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var row = dataGridView1.SelectedRows[0];
                var id = (int)row.Cells["Id"].Value;
                var res = MessageBox.Show("是否确定删除", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                if (res.Equals(DialogResult.Yes))
                {
                    var ds = String.Format("delete from Articles where Id = {0}", id);
                    var dd = DbHelper.AddOrAddOrEditOrDeleteSave(ds);
                    if (dd > 0)
                    {
                        var newTitleName = textBox1.Text;
                        var findString = string.Format("select * from Articles where Title like '%{0}%'", newTitleName);
                        var dt = DbHelper.GetDataTable(findString);
                        dataGridView1.DataSource = dt;
                    }
                }
            }
            else
            {
                MessageBox.Show("没有选定任何行!", "提示");
            }
        }
    }
}
